﻿using CrystalDecisions.Shared.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Class;

namespace WebApplication2
{
    public partial class Summary : System.Web.UI.Page
    {
        int language = 0;
        DataForSummaryScreen DFSS = new DataForSummaryScreen();
        protected void Page_Load(object sender, EventArgs e)
        {
            language = (int)Session["Language"];
            DFSS = (DataForSummaryScreen)Session["DataForSummaryScreen"];
            ddlLoc.Items.Add("Please Select One");
            ddlWard.Items.Add("Please Select One");
            ddlMode.Items.Add("Please Select One");
            ddlAddOn.Items.Add("Please Select One");
            ddlType.Items.Add("Please Select One");
            GetScreenLabel();
            GetScreenData(DFSS);
        }

        public void GetScreenLabel()
        {
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXSummaryLabel?LanguageId=" + language);
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"FMTXSummaryLabel\":" + json + "}";
            string bp1="";
            string bp2="";
      
            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperSummaryLabel>(json);

            foreach (var item in keysWrapper.fmtxsummarylabel)
            {
                if (item.FormLabelCode == "TxSc-Title-Sum")
                {
                    lblSummary.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-GpPI-1")
                {
                    lblPatientInformation.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-GpTxS")
                {
                    lblTreatmentStatus.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Alert")
                {
                    lblAlert.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-PreHD")
                {
                    lblPreHD.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-HDRun")
                {
                    lblHdRUn.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-PostHD")
                {
                    lblPostHd.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Algy")
                {
                    lblAllergy.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Comp")
                {
                    lblEvents.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-NotTx")
                {
                    lblNotTreatment.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-LocWdBd")
                {
                    lblLoc.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Signby")
                {
                    lblSignOff.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Mode")
                {
                    lblMode.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Odate")
                {
                    lblOrderDate.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-DArrive")
                {
                    lblArrivalDate.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-TArrfmt")
                {
                    lblhhmm.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-DWt")
                {
                    lblDryWeight.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-IDWG")
                {
                    lblIDWG.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Runno")
                {
                    lblRunNo.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-VDW")
                {
                    lblVarianceDryWt.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-NUF")
                {
                    lblNetUf.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-SpInst")
                {
                    lblSpecialInstructions.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-GpTxO")
                {
                    lblOverview.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-GpTxV")
                {
                    lblVitals.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Range")
                {
                    lblRange.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Avg")
                {
                    lblAvg.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Pre")
                {
                    lblPre.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Post")
                {
                    lblPost.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-BPS")
                {
                    lblBPs.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Wt")
                {
                    lblWeight.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-BPD")
                {
                    lblBPd.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-BP1")
                {
                    bp1 = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Pulse1")
                {
                    lblBpPulse.Text = bp1 + "/" + item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-QB")
                {
                    lblBloodFlow.Text =item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Qbu")
                {
                    lblMl.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-BP2")
                {
                    bp2 = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Pulse2")
                {
                    lblBpPulse2.Text = bp2 + "/" + item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Pump")
                {
                    lblBloodPump.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Pumpu")
                {
                    lblMl2.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-1Hr")
                {
                    lbl1st.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-AP")
                {
                    lblArtPressure.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Apu")
                {
                    lblmmHg3.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-2Hr")
                {
                    lbl2nd.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-VP")
                {
                    lblVenPressure.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-Vpu")
                {
                    lblmmHg4.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-3Hr")
                {
                    lbl3rd.Text = item.Language.ToString();
                }

                if (item.FormLabelCode == "TxSc-GpTxN")
                {
                    lblNursingNotes.Text = item.Language.ToString();
                }

                if (item.ModeID != 0)
                {
                    ddlMode.Items.Add(item.Mode_Language.ToString());
                }

                if (item.AddOnId != 0)
                {
                    ddlAddOn.Items.Add(item.Addon_Language.ToString());
                }

                if (item.TreatmentLocID != 0)
                {
                    ddlLoc.Items.Add(item.TreatmentLoc.ToString());
                }
                if (item.WardID != 0)
                {

                    ddlWard.Items.Add(item.Ward_Name.ToString());
                }

                if (item.DialysisTypeID != 0)
                {
                    ddlType.Items.Add(item.DialysisType.ToString());
                }
            }
        }

        public void GetScreenData(DataForSummaryScreen d)
        {
            WebRequest req = WebRequest.Create("http://localhost:49317/api/FMTXSummary?PatientID=" + d.PatientID + "&&Arrival_Time=" + d.Arrival_Time);
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            JObject o = JObject.Parse(json);
            tbxAlert.Text = o["Alert"].ToString();
            int locID = Convert.ToInt32(o["Treatment_Loc"]);
            int wardId = Convert.ToInt32(o["Ward_Name"]);
            tbxBed.Text = o["BedNo"].ToString();
            int modeID = Convert.ToInt32(o["ModeID"]);
            int AddOnId = Convert.ToInt32(o["AddOnId"]);
            int DialysisTypeID = Convert.ToInt32(o["Dialysis_Type"]);
            tbxRunNo.Text = o["Run_No"].ToString();
            tbxDryWeight.Text = o["Target_Weight"].ToString();
            decimal Last_Post_Weight = Convert.ToDecimal(o["Last_Post_Weight"]);
            decimal weight = Convert.ToDecimal(o["weight"]);
            decimal IDWG = Last_Post_Weight - weight;
            tbxIDWG.Text = IDWG.ToString();
            tbxVarianceDryWt.Text = o["Variance_frm_Target_Weight"].ToString();
            tbxNetUf.Text = o["Net_UF"].ToString();
            tbxBPsMin.Text = o["MinBPSYS"].ToString();
            tbxBPsMax.Text = o["MaxBPSYS"].ToString();
            tbxBPdMin.Text = o["MinBPDias"].ToString();
            tbxBPdMax.Text = o["MaxBPDias"].ToString();
            tbxBloodFlowMin.Text = o["MinBFR"].ToString();
            tbxBloodFlowMax.Text = o["MaxBFR"].ToString();
            tbxBloodPumpMin.Text = o["MaxBPump"].ToString();
            tbxBloodPumpMax.Text = o["MinBPump"].ToString();
            tbxArtPressureMin.Text = o["MinArtP"].ToString();
            tbxArtPressureMax.Text = o["MaxArtP"].ToString();
            tbxVenPressureMin.Text = o["MaxVenP"].ToString();
            tbxVenPressureMax.Text = o["MinVenP"].ToString();
            nursingNotes.InnerHtml = o["HD_Comments"].ToString();

            WebRequest req1 = WebRequest.Create("http://192.168.1.156/api/FMTXSummaryLabel?LanguageId=" + language);
            req1.ContentType = "application/json";
            WebResponse resp1 = req1.GetResponse();
            Stream stream1 = resp1.GetResponseStream();
            StreamReader re1 = new StreamReader(stream1);
            String json1 = re1.ReadToEnd();
            json = "{\"FMTXSummaryLabel\":" + json1 + "}";
            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperSummaryLabel>(json);

            foreach (var item in keysWrapper.fmtxsummarylabel)
            {
                if (item.TreatmentLocID == locID)
                {
                    ddlLoc.Text = item.TreatmentLoc.ToString();
                }

                if (item.TreatmentLocID==0 && item.LabelID==0 && item.AddOnId==0 && item.DialysisTypeID==0 && item.ModeID==0 &&item.User_Id==0)
                {
                    if (wardId != 0)
                    {
                        if (item.WardID == wardId)
                        {
                            ddlWard.Text = item.Ward_Name.ToString();
                        }
                    }
                    else
                    {
                        ddlWard.SelectedIndex = -1;
                    }
                }

                if (item.ModeID == modeID)
                {
                    ddlMode.Text = item.Mode_Language.ToString();
                }

                if (item.TreatmentLocID == 0 && item.LabelID == 0 && item.WardID == 0 && item.DialysisTypeID == 0 && item.ModeID == 0 && item.User_Id == 0)
                {
                    if (AddOnId != 0)
                    {
                        if (item.AddOnId == AddOnId)
                        {
                            ddlAddOn.Text = item.Ward_Name.ToString();
                        }
                    }
                    else
                    {
                        ddlAddOn.SelectedIndex = -1;
                    }
                }

                if (item.TreatmentLocID == 0 && item.LabelID == 0 && item.WardID == 0 && item.AddOnId == 0 && item.ModeID == 0 && item.User_Id == 0)
                {
                    if (DialysisTypeID != 0)
                    {
                        if (item.DialysisTypeID == DialysisTypeID)
                        {
                            ddlType.Text = item.DialysisType.ToString();
                        }
                    }
                    else
                    {
                        ddlType.SelectedIndex = -1;
                    }
                }
            }
        }
        
    }
}
